$(function(){
    
    if(navigator.geolocation == undefined){
        alert("이 브라우저는 geolocation을 지원하지 않습니다.");
        return;
    }
    
    var geocoder = new google.maps.Geocoder();
    var myOptions;
    var latlng;
    var map = new google.maps.Map($("#map_canvas")[0],null);
    var marker = new google.maps.Marker(); 
    var infowindow = new google.maps.InfoWindow();
    
    marker.setIcon("../res/drawable-hdpi/marker.png");
    
    navigator.geolocation.getCurrentPosition(successCallback, errorCallback);

    function successCallback(position){
        
        latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        myOptions = {
          zoom: 18,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        map.setOptions(myOptions);
        
        if (geocoder) {
            geocoder.geocode({'latLng': latlng}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                  map.setZoom(18);
                  marker.setPosition(latlng);
                  marker.setMap(map);
                  infowindow.setContent(results[0].formatted_address);
                  infowindow.open(map, marker);
                  window.android.callAndroid(latlng.toString());
                }
              } else {
              
               	  map.setZoom(18);
                  marker.setPosition(latlng);
                  marker.setMap(map);
                  
                window.android.callAndroid("Error");
                
              }
            });
          }
    }
    
    function errorCallback(){

        latlng = new google.maps.LatLng(-34.397, 150.644);
        myOptions = {
          zoom: 8,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        map.setOptions(myOptions);
        window.android.callAndroid("errorCallback");
    }

    google.maps.event.addListener(map,'click',function(event){
        
        latlng = new google.maps.LatLng(event.latLng.lat(),event.latLng.lng());
        
        if (geocoder) {
            geocoder.geocode({'latLng': latlng}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                  map.setZoom(18);
                  marker.setPosition(latlng);
                  marker.setMap(map);
                  infowindow.setContent(results[0].formatted_address);
                  infowindow.open(map, marker);
                  window.android.callAndroid(latlng.toString());
                }
              } else {
                 window.android.callAndroid("Error");
              }
            });
          }
    });
    
    $('#searchButton').bind('click',function(results, status){
        var address = document.getElementById("address").value;
        if (geocoder) {
          geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              map.setCenter(results[0].geometry.location);
                  map.setZoom(18);
                  marker.setMap(map); 
                  marker.setPosition(results[0].geometry.location);
                  infowindow.setContent(results[0].formatted_address);
                  infowindow.open(map, marker);
                  latlng = marker.getPosition();
                  window.android.callAndroid(latlng.toString());
            } else {
               window.android.callAndroid("addressError");
            }
          });
        }
    });

});